package com.yn.xgame.util;

import com.yn.xgame.GlobalSetting;
import com.yn.xgame.XAssert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;
import java.util.regex.Matcher;

/**
 * Utils
 * Created by wangboo on 2017/7/12.
 */
public class Utils {

    private static final Logger logger = LoggerFactory.getLogger(Utils.class);
    public static final Charset charset = Charset.forName("utf8");

    public static void main(String[] args) {
        System.out.println(getNo("/center/12"));
//        System.out.println(getPrefixWord("center12"));
        String str = "niuniu12";
        int id = getNo(str);
        System.out.println(id);
    }

    public static int getNo(String path) {
        Matcher matcher = GlobalSetting.P_ZK_MATCH_NO.matcher(path);
        if (matcher.matches()) {
            String str = matcher.group(1);
            try {
                return Integer.parseInt(str);
            } catch (Exception e) {
                logger.error("key: {} not find gate number", path);
            }
        }
        logger.error("key: {} not find gate number", path);
        return -1;
    }

    public static String getPrefixWord(String str) {
        Matcher matcher = GlobalSetting.P_DROP_SUFFIX_NUMBER.matcher(str);
        if (matcher.matches()) {
            return matcher.group(1);
        }
        return null;
    }

    public static int getInt(byte[] bin) {
        XAssert.isTrue(bin.length == 4, bin);
        return (bin[3] << 24) + (bin[2] << 16) + (bin[1] << 8) + bin[0];
    }

    public static byte[] getIntByte(int v) {
        byte[] bin = new byte[4];
        bin[0] = (byte) ((v >> 24) & 0xff);
        bin[1] = (byte) ((v >> 16) & 0xff);
        bin[2] = (byte) ((v >> 8) & 0xff);
        bin[3] = (byte) (v & 0xff);
        return bin;
    }

    public static long getLong(byte[] bin) {
        XAssert.isTrue(bin.length == 8, bin);
        long num = 0;
        for (int ix = 0; ix < 8; ++ix) {
            num <<= 8;
            num |= (bin[ix] & 0xff);
        }
        return num;
    }

    public static byte[] getLongByte(long v) {
        byte[] byteNum = new byte[8];
        for (int ix = 0; ix < 8; ++ix) {
            int offset = 64 - (ix + 1) * 8;
            byteNum[ix] = (byte) ((v >> offset) & 0xff);
        }
        return byteNum;
    }



}
